/**
 * Created by szc on 2016/12/16.
 */

class CheckBox extends egret.DisplayObjectContainer {
    private _bg: ImageBitmap;
    private _ok: ImageBitmap;
    public signal = 1;
    public constructor() {
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }
    private onAddToStage(event: egret.Event) {
        var _bgCon = [];
        _bgCon['url'] = 'check_box_bg_png';
        _bgCon['width'] = this.stage.stageWidth * 0.03;
        _bgCon['height'] = _bgCon['width']
        this._bg = new ImageBitmap(_bgCon);
        this.addChild(this._bg.getImgPoint());
        this._bg.getImgPoint().touchEnabled = true;

        var okCon = [];
        okCon['url'] = 'check_box_ok_png';
        okCon['width'] = this.stage.stageWidth * 0.03;
        okCon['height'] = okCon['width'] * 1.1;
        okCon['y'] = -5;
        okCon["x"] = 5;
        this._ok = new ImageBitmap(okCon);
        this.addChild(this._ok.getImgPoint());
        this.setClose();
        this._bg.getImgPoint().addEventListener(egret.TouchEvent.TOUCH_TAP, this.switchState, this);
    }

    /**
     * 向外提供相应的接口数据
     * @returns {CheckBox}
     */
    public getImgPoint(): egret.DisplayObjectContainer {
        return this;
    }

    public setClose(): void {
        if (this.contains(this._ok.getImgPoint())) {
            this.removeChild(this._ok.getImgPoint());
            this.signal = 0;
        }
    }

    public setOpen():void{
          if (!this.contains(this._ok.getImgPoint())) {
          this.addChild(this._ok.getImgPoint());
            this.signal = 1;
        }
    }

    public switchState():void{
        if(this.signal==1){
            this.setClose();
        } else{
            this.setOpen();
        }
    }

}